/**
 * ParticleSystem - 粒子系统
 * 实现各种视觉特效
 */

// 粒子类型配置
const PARTICLE_TYPES = {
  resource: {
    lifetime: 1000,
    size: 20,
    speed: 2,
    gravity: -0.5
  },
  upgrade: {
    lifetime: 2000,
    size: 30,
    speed: 1,
    gravity: 0
  },
  celebration: {
    lifetime: 3000,
    size: 25,
    speed: 1.5,
    gravity: 0.2
  },
  sparkle: {
    lifetime: 1500,
    size: 15,
    speed: 0.5,
    gravity: 0
  }
}

class ParticleSystem {
  constructor(ctx) {
    this.ctx = ctx
    this.particles = []
    this.maxParticles = 200
  }

  /**
   * 添加粒子
   */
  add(type, x, y, options = {}) {
    const config = PARTICLE_TYPES[type]
    if (!config) return
    
    // 限制粒子数量
    if (this.particles.length >= this.maxParticles) {
      this.particles.shift()
    }
    
    const particle = {
      type,
      x,
      y,
      vx: options.vx || (Math.random() - 0.5) * config.speed,
      vy: options.vy || -Math.abs(Math.random()) * config.speed,
      lifetime: config.lifetime,
      createdAt: Date.now(),
      size: config.size,
      gravity: config.gravity,
      icon: options.icon || '✨',
      color: options.color || '#FFD700',
      alpha: 1,
      rotation: Math.random() * Math.PI * 2,
      rotationSpeed: (Math.random() - 0.5) * 0.1
    }
    
    this.particles.push(particle)
  }

  /**
   * 渲染所有粒子
   */
  render(timestamp) {
    this.particles = this.particles.filter(particle => {
      const age = timestamp - particle.createdAt
      if (age > particle.lifetime) return false
      
      // 更新粒子位置
      particle.x += particle.vx
      particle.y += particle.vy
      particle.vy += particle.gravity
      particle.rotation += particle.rotationSpeed
      
      // 计算透明度
      const progress = age / particle.lifetime
      particle.alpha = 1 - progress
      
      // 绘制粒子
      this.drawParticle(particle, progress)
      
      return true
    })
  }

  /**
   * 绘制单个粒子
   */
  drawParticle(particle, progress) {
    const { x, y, type, alpha, size, icon, color, rotation } = particle
    
    this.ctx.setGlobalAlpha(alpha)
    
    switch (type) {
      case 'resource':
        this.drawResourceParticle(particle, progress)
        break
      case 'upgrade':
        this.drawUpgradeParticle(particle, progress)
        break
      case 'celebration':
        this.drawCelebrationParticle(particle, progress)
        break
      case 'sparkle':
        this.drawSparkleParticle(particle, progress)
        break
    }
    
    this.ctx.setGlobalAlpha(1)
  }

  /**
   * 绘制资源粒子
   */
  drawResourceParticle(particle, progress) {
    const { x, y, icon, size } = particle
    
    // 绘制图标
    this.ctx.setFontSize(size)
    this.ctx.setTextAlign('center')
    this.ctx.setTextBaseline('middle')
    this.ctx.fillText(icon, x, y)
    
    // 绘制光晕
    const glowSize = size * (1 + progress * 0.5)
    this.ctx.setFillStyle(`rgba(255, 215, 0, ${0.3 * (1 - progress)})`)
    this.ctx.beginPath()
    this.ctx.arc(x, y, glowSize, 0, Math.PI * 2)
    this.ctx.fill()
  }

  /**
   * 绘制升级粒子
   */
  drawUpgradeParticle(particle, progress) {
    const { x, y, size, color } = particle
    
    // 扩散的圆环
    const radius = size * (1 + progress * 2)
    const lineWidth = size * 0.2 * (1 - progress)
    
    this.ctx.setStrokeStyle(color)
    this.ctx.setLineWidth(lineWidth)
    this.ctx.beginPath()
    this.ctx.arc(x, y, radius, 0, Math.PI * 2)
    this.ctx.stroke()
    
    // 中心星星
    if (progress < 0.5) {
      this.drawStar(x, y, size * (1 - progress * 2), color)
    }
  }

  /**
   * 绘制庆祝粒子
   */
  drawCelebrationParticle(particle, progress) {
    const { x, y, icon, size, rotation } = particle
    
    // 旋转的图标
    this.ctx.save()
    this.ctx.translate(x, y)
    this.ctx.rotate(rotation)
    
    this.ctx.setFontSize(size * (1 + Math.sin(progress * Math.PI) * 0.3))
    this.ctx.setTextAlign('center')
    this.ctx.setTextBaseline('middle')
    this.ctx.fillText(icon, 0, 0)
    
    this.ctx.restore()
  }

  /**
   * 绘制闪光粒子
   */
  drawSparkleParticle(particle, progress) {
    const { x, y, size, color } = particle
    
    // 闪烁的星星
    const scale = Math.sin(progress * Math.PI)
    this.drawStar(x, y, size * scale, color)
  }

  /**
   * 绘制星星
   */
  drawStar(x, y, size, color) {
    this.ctx.setFillStyle(color)
    this.ctx.beginPath()
    
    for (let i = 0; i < 5; i++) {
      const angle = (i * 4 * Math.PI) / 5 - Math.PI / 2
      const radius = i % 2 === 0 ? size : size / 2
      const px = x + Math.cos(angle) * radius
      const py = y + Math.sin(angle) * radius
      
      if (i === 0) {
        this.ctx.moveTo(px, py)
      } else {
        this.ctx.lineTo(px, py)
      }
    }
    
    this.ctx.closePath()
    this.ctx.fill()
  }

  /**
   * 添加资源收集特效
   */
  addResourceCollect(x, y, resourceType) {
    const icons = {
      wood: '🪵',
      food: '🌾',
      gold: '💰'
    }
    
    const icon = icons[resourceType] || '✨'
    
    // 添加多个粒子
    for (let i = 0; i < 5; i++) {
      this.add('resource', x, y, {
        icon,
        vx: (Math.random() - 0.5) * 3,
        vy: -Math.random() * 3
      })
    }
  }

  /**
   * 添加升级特效
   */
  addUpgradeEffect(x, y) {
    // 添加多个圆环
    for (let i = 0; i < 3; i++) {
      setTimeout(() => {
        this.add('upgrade', x, y, {
          color: ['#FFD700', '#FFA500', '#FF6347'][i]
        })
      }, i * 200)
    }
    
    // 添加星星粒子
    for (let i = 0; i < 8; i++) {
      const angle = (i / 8) * Math.PI * 2
      this.add('sparkle', x, y, {
        vx: Math.cos(angle) * 2,
        vy: Math.sin(angle) * 2,
        color: '#FFD700'
      })
    }
  }

  /**
   * 添加庆祝特效
   */
  addCelebrationEffect(x, y) {
    const icons = ['🎉', '🎊', '✨', '⭐', '🌟']
    
    // 添加多个庆祝粒子
    for (let i = 0; i < 15; i++) {
      setTimeout(() => {
        this.add('celebration', x, y, {
          icon: icons[Math.floor(Math.random() * icons.length)],
          vx: (Math.random() - 0.5) * 4,
          vy: -Math.random() * 4
        })
      }, i * 100)
    }
  }

  /**
   * 清空所有粒子
   */
  clear() {
    this.particles = []
  }

  /**
   * 获取粒子数量
   */
  getCount() {
    return this.particles.length
  }
}

export default ParticleSystem
export { PARTICLE_TYPES }
